<script lang="ts" setup>
import { ref } from 'vue'
import { GetOptionData, GetUserData } from '@/api/modules/givefacialmaskact'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('givefacialmaskactUser').currentPage

const findFrom = ref({
  ...(cacheData.GetCacheData('givefacialmaskactUser').fromData),
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.ActMianmoId && ({ ActMianmoId: findFrom.value.ActMianmoId })),
    ...(findFrom.value.UserId !== '' && ({ UserId: findFrom.value.UserId })),
  }
  await GetUserData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('givefacialmaskactUser').currentSize)
  cacheData.SetCacheData('givefacialmaskactUser', PageNum, cacheData.GetCacheData('givefacialmaskactUser').currentSize, findFrom.value)
}

function Switchover() {
  router.push({
    name: 'givefacialmaskactUserDetail',
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('givefacialmaskactUser', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('givefacialmaskactUser').currentSize)
  cacheData.SetCacheData('givefacialmaskactUser', val, cacheData.GetCacheData('givefacialmaskactUser').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('givefacialmaskactUser').currentPage || 1
})

onMounted(async () => {
  try {
    await GetOptionData().then((res: any) => {
      dataList.option = res.data
    })
    await GetDataList(cacheData.GetCacheData('givefacialmaskactUser').currentPage, cacheData.GetCacheData('givefacialmaskactUser').currentSize)
  }
  catch (error) {
    console.error(error)
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="面膜活动">
                <el-select v-model="findFrom.ActMianmoId" placeholder="请选择面膜活动" style="width: 240px;">
                  <el-option
                    v-for="item in dataList.option"
                    :key="item.id"
                    :label="`${item.id}---${item.title}`"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button @click="Switchover">
          查看分享记录
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="actMianmoId" label="面膜活动ID" width="100" />
        <el-table-column align="center" prop="subscriber.userId" label="用户ID" width="100" />
        <el-table-column align="center" prop="subscriber" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriber?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriber?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column header-align="center" prop="subscriber.scOpenId" label="OpenId" width="300" />
        <el-table-column align="center" prop="addTime" label="参与时间" width="180" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="180" />
        <el-table-column label="已领取券" min-width="440">
          <template #default="scope">
            <el-scrollbar>
              <div class="ticket-box">
                <div class="user-name">
                  <el-avatar shape="square" :size="60" style="margin-right: 10px;" :src="imgDomain + scope.row.gotTicketList[0].couponInfo?.picUrl" />
                  <div>
                    <p style="margin: 0;">
                      券ID：{{ scope.row.gotTicketList[0].couponInfo?.id }}
                    </p>
                    <p style="margin: 0;">
                      券名称：{{ scope.row.gotTicketList[0].couponInfo?.name }}
                    </p>
                    <p style="margin: 0;">
                      券类型：{{ scope.row.gotTicketList[0].couponInfo?.type === 1 ? '赠品券' : scope.row.gotTicketList[0].couponInfo?.type === 2 ? '奖品券' : '满减券' }}
                    </p>
                  </div>
                  X {{ scope.row.gotTicketList.length }}
                </div>
              </div>
            </el-scrollbar>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('givefacialmaskactUser').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 80px;
    margin-right: 10px;
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;
  margin-right: 25px;

  div {
    text-align: left;
  }
}

.ticket-box {
  display: flex;
  flex-wrap: wrap;
}
</style>
